package com.example.service;


import cn.hutool.core.util.ObjectUtil;
import com.example.common.ResultCode;
import com.example.dao.TeacherInfoDao;
import com.example.dao.ZhuanyeInfoDao;
import com.example.entity.*;
import com.example.exception.CustomException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TeacherInfoService {
    @Resource
    private TeacherInfoDao teacherInfoDao;
    @Autowired
    private ZhuanyeInfoDao zhuanyeInfoDao;

//    public void register(TeacherInfo teacherInfo) {
//        //用户名不重复
//        TeacherInfo info = teacherInfoDao.findByName(teacherInfo.getName());
//        if(ObjectUtil.isNotEmpty(info)){
//            throw new CustomException(ResultCode.USER_EXIST_ERROR);
//        }
//        //插入数据
//        teacherInfoDao.insertSelective(teacherInfo);
//    }

    public Account login(String name, String password) {
        //通过用户名和密码去数据库里查一条数据
        TeacherInfo teacherInfo=teacherInfoDao.findByNameAndPassword(name,password);

        if(ObjectUtil.isEmpty(teacherInfo)){
            throw new CustomException("-1","用户名、密码或角色错误");
        }
        return  teacherInfo;
    }

    public TeacherInfo findById(long id) {
        return teacherInfoDao.selectById(id);
    }

    public void update(TeacherInfo teacherInfo) {
        //teacherInfoDao.updateByPrimaryKeySelective(teacherInfo);
        teacherInfoDao.updateById(teacherInfo.getId(),teacherInfo.getName(),teacherInfo.getPassword(),teacherInfo.getSex(),teacherInfo.getAge(),teacherInfo.getZhicheng(),teacherInfo.getLevel(),teacherInfo.getZhuanyeid());
    }

    public List<TeacherInfo> findAll() {
        return teacherInfoDao.selectAllTeacher();

    }

    public void add(TeacherInfo teacherInfo) {
        TeacherInfo info=teacherInfoDao.findByName(teacherInfo.getName());
        if(ObjectUtil.isNotEmpty(info)){
            throw  new CustomException(ResultCode.USER_EXIST_ERROR);
        }
        //默认密码为123456
        if(ObjectUtil.isEmpty(teacherInfo.getPassword())){
            teacherInfo.setPassword("123456");

        }
        if(ObjectUtil.isEmpty(teacherInfo.getLevel())) {
            teacherInfo.setLevel(2);
        }
        teacherInfoDao.insertSelective(teacherInfo);
    }

    public void deleteById(Long id) {
        teacherInfoDao.deleteById(id);
    }

    public PageInfo<TeacherInfo> findPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TeacherInfo > infos = teacherInfoDao.selectAllTeacher();
        for(TeacherInfo teacherInfo : infos) {
            if (ObjectUtil.isNotEmpty(teacherInfo.getZhuanyeid())) {
                ZhuanyeInfo zhuanyeInfo = zhuanyeInfoDao.selectById(teacherInfo.getZhuanyeid());
                teacherInfo.setZhuanyeName(zhuanyeInfo.getName());
            }
        }
        return PageInfo.of(infos);
    }

    public PageInfo<TeacherInfo> findPageName(Integer pageNum, Integer pageSize, String name) {
        PageHelper.startPage(pageNum,pageSize);
        List<TeacherInfo> infos =teacherInfoDao.findByNamePage(name);
        for(TeacherInfo teacherInfo : infos) {
            if (ObjectUtil.isNotEmpty(teacherInfo.getZhuanyeid())) {
                ZhuanyeInfo zhuanyeInfo = zhuanyeInfoDao.selectById(teacherInfo.getZhuanyeid());
                teacherInfo.setZhuanyeName(zhuanyeInfo.getName());
            }
        }
        return PageInfo.of(infos);
    }
}
